/*     */ package com.zimbra.cs.mailbox.acl;
/*     */ 
/*     */ import com.google.common.collect.ArrayListMultimap;
/*     */ import com.google.common.collect.Multimap;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.ShareInfoData;
/*     */ import com.zimbra.cs.db.DbPendingAclPush;
/*     */ import com.zimbra.cs.mailbox.ACL;
/*     */ import com.zimbra.cs.mailbox.ACL.Grant;
/*     */ import com.zimbra.cs.mailbox.Folder;
/*     */ import com.zimbra.cs.mailbox.MailItem;
/*     */ import com.zimbra.cs.mailbox.MailItem.Type;
/*     */ import com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.MailboxManager;
/*     */ import com.zimbra.cs.util.ZimbraApplication;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Collection;
/*     */ import java.util.Date;
/*     */ import java.util.HashSet;
/*     */ import java.util.Iterator;
/*     */ import java.util.List;
/*     */ import java.util.Map.Entry;
/*     */ import java.util.Set;
/*     */ import java.util.TimerTask;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class AclPushTask
/*     */   extends TimerTask
/*     */ {
/*  54 */   private static boolean supported = ZimbraApplication.getInstance().supports(AclPushTask.class);
/*     */   
/*     */ 
/*     */ 
/*     */   public void run() {}
/*     */   
/*     */ 
/*     */   public static synchronized void doWork()
/*     */   {
/*  63 */     if (!supported)
/*  64 */       return;
/*  65 */     ZimbraLog.misc.debug("Starting pending ACL push");
/*     */     
/*  67 */     Multimap<Integer, List<Integer>> currentItemIdsProcessed = ArrayListMultimap.create();
/*     */     try
/*     */     {
/*  70 */       Date now = new Date();
/*  71 */       Multimap<Integer, Integer> mboxIdToItemIds = DbPendingAclPush.getEntries(now);
/*     */       
/*  73 */       for (Iterator i$ = mboxIdToItemIds.keySet().iterator(); i$.hasNext();) { int mboxId = ((Integer)i$.next()).intValue();
/*     */         
/*  75 */         List<Integer> itemsProcessed = new ArrayList();
/*     */         Mailbox mbox;
/*  77 */         try { mbox = MailboxManager.getInstance().getMailboxById(mboxId);
/*     */         } catch (ServiceException e) {
/*  79 */           ZimbraLog.misc.info("Exception occurred while getting mailbox for id %s during ACL push", Integer.valueOf(mboxId), e); }
/*  80 */         continue;
/*     */         
/*  82 */         Collection<Integer> itemIds = mboxIdToItemIds.get(Integer.valueOf(mboxId));
/*  83 */         MailItem[] items = null;
/*     */         try {
/*  85 */           items = mbox.getItemById(null, itemIds, MailItem.Type.UNKNOWN);
/*     */         }
/*     */         catch (MailServiceException.NoSuchItemException e) {
/*  88 */           if (itemIds.size() > 1) {
/*  89 */             List<MailItem> itemList = new ArrayList();
/*  90 */             for (Iterator i$ = itemIds.iterator(); i$.hasNext();) { int itemId = ((Integer)i$.next()).intValue();
/*     */               try {
/*  92 */                 itemList.add(mbox.getItemById(null, itemId, MailItem.Type.UNKNOWN));
/*     */               }
/*     */               catch (MailServiceException.NoSuchItemException ignored) {}
/*     */             }
/*  96 */             items = (MailItem[])itemList.toArray(new MailItem[itemList.size()]);
/*     */           }
/*     */         }
/*     */         
/* 100 */         Account account = mbox.getAccount();
/* 101 */         String[] existingSharedItems = account.getSharedItem();
/* 102 */         Set<String> updatedSharedItems = new HashSet();
/*     */         
/* 104 */         for (String sharedItem : existingSharedItems) {
/* 105 */           ShareInfoData shareData = AclPushSerializer.deserialize(sharedItem);
/*     */           
/* 107 */           if (!itemIds.contains(Integer.valueOf(shareData.getItemId()))) {
/* 108 */             updatedSharedItems.add(sharedItem);
/*     */           }
/*     */         }
/*     */         
/* 112 */         if (items != null)
/* 113 */           for (MailItem item : items)
/* 114 */             if (item != null)
/*     */             {
/*     */ 
/*     */ 
/* 118 */               if ((item instanceof Folder))
/*     */               {
/*     */ 
/* 121 */                 ACL acl = item.getACL();
/* 122 */                 if (acl != null)
/*     */                 {
/*     */ 
/* 125 */                   for (ACL.Grant grant : acl.getGrants()) {
/* 126 */                     updatedSharedItems.add(AclPushSerializer.serialize(item, grant));
/*     */                   }
/* 128 */                   itemsProcessed.add(Integer.valueOf(item.getId()));
/* 129 */                   currentItemIdsProcessed.put(Integer.valueOf(mboxId), itemsProcessed);
/*     */                 }
/*     */               } }
/* 132 */         account.setSharedItem((String[])updatedSharedItems.toArray(new String[updatedSharedItems.size()]));
/*     */       }
/* 134 */       DbPendingAclPush.deleteEntries(now);
/*     */     }
/*     */     catch (ServiceException e) {
/* 137 */       ZimbraLog.misc.warn("Error during ACL push task", e);
/*     */ 
/*     */     }
/*     */     catch (Throwable t)
/*     */     {
/*     */       try
/*     */       {
/* 144 */         deleteDbAclEntryForProcessedItems(currentItemIdsProcessed);
/*     */       } catch (ServiceException e) {
/* 146 */         ZimbraLog.misc.warn("Error during ACL push task and deleting ACL push entry.");
/*     */       }
/* 148 */       ZimbraLog.misc.warn("Error during ACL push task", t);
/*     */     }
/* 150 */     ZimbraLog.misc.debug("Finished pending ACL push");
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private static void deleteDbAclEntryForProcessedItems(Multimap<Integer, List<Integer>> currentItemIdsProcessed)
/*     */     throws ServiceException
/*     */   {
/* 160 */     if (currentItemIdsProcessed.size() != 0) {
/* 161 */       Collection<Map.Entry<Integer, List<Integer>>> mailboxIds = currentItemIdsProcessed.entries();
/* 162 */       for (Map.Entry<Integer, List<Integer>> entry : mailboxIds) {
/* 163 */         mboxId = ((Integer)entry.getKey()).intValue();
/* 164 */         List<Integer> itemIds = (List)entry.getValue();
/* 165 */         for (i$ = itemIds.iterator(); i$.hasNext();) { int itemId = ((Integer)i$.next()).intValue();
/* 166 */           DbPendingAclPush.deleteEntry(mboxId, itemId);
/*     */         }
/*     */       }
/*     */     }
/*     */     int mboxId;
/*     */     Iterator i$;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/mailbox/acl/AclPushTask.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */